package com.example.demo.web;

import com.example.demo.mapper.firstsqlserver.MonthRecordMapper;
import com.example.demo.pojo.MonthRecord;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

@Controller
public class MonthRecordController {

    @Autowired
    MonthRecordMapper mapper;

    @RequestMapping("/listMonthRecord")
    public String listMonthRecord() {
        return "listMonthRecord";
    }

    @ResponseBody
    @RequestMapping("/listMonthRecordData")
    public List<MonthRecord> listData(@RequestParam(value = "start", defaultValue = "1")int start, @RequestParam(value = "size", defaultValue = "6")int size) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cale = Calendar.getInstance();
        String firstDay, lastDay;
        // 获取当月的第一天
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        firstDay = sdf.format(cale.getTime());
        // 获取当月最后一天
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        lastDay = sdf.format(cale.getTime());
        //System.out.println("当月第一天：" + firstDay + "，当月最后一天：" + lastDay);
        String startTime = firstDay + " 00:00:00";
        String endTime = lastDay + " 23:59:59";
        List<MonthRecord> cs1 = mapper.listMonthRecord(startTime, endTime);
        PageHelper.startPage(start, size);
        List<MonthRecord> cs = mapper.listMonthRecord(startTime, endTime);
        for (int i = 0; i < cs.size(); i++) {
            //cs.get(i).setId(i + 1);
            cs.get(i).setTotal(cs1.size());
        }
        PageInfo<MonthRecord> pages = new PageInfo<>(cs);
        System.out.println(pages.getList().size());
        return pages.getList();
    }

}
